﻿using SNPAD;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 

namespace SNPAD
{
    public partial class Login_Form : Form
    {
        private static readonly object Locker = new object();
        private static int _sn = 0;
        private static string username;
        private int UserCount = 1;

        public static string Username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
            }
        }

        public Login_Form()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Settings_Form sf = new Settings_Form();
            sf.ShowDialog();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            ErrorProvider errInfo = new ErrorProvider();
            errInfo.Clear();
            

            // string pwd = CrytMd5.GetMd5Str(this.txt_PassWord.Text.Trim());
            string pwd =  this.txt_PassWord.Text.Trim() ;
            if (string.IsNullOrEmpty(txt_UserName.Text.Trim()))
            {
                try
                {
                    errInfo.SetError(txt_UserName, "用户编码不能为空！");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            if (String.IsNullOrEmpty(txt_PassWord.Text.Trim()))
            {
                try
                {
                    errInfo.SetError(txt_PassWord, "用户密码不能为空！");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            else
            {
                try
                {
                    skinProgressIndicator1.Show();
                    skinProgressIndicator1.Start();
                    if (!backgroundWorker1.IsBusy)
                        backgroundWorker1.RunWorkerAsync();

                    //int i = Convert.ToInt32(OleDbHelper3.ExecuteScalar("select count( *) from Operators where curr_user_id='" + txt_UserName.Text + "' and user_password='" + txt_PassWord.Text + "'", null));
                    //if (i > 0)
                    //{
                    //    GlobalVar.CurrOperator = txt_UserName.Text.Trim();//存储用户ID

                    //        this.Hide();
                    //        //////Main2 m = new Main2();
                    //        //////username = txt_UserName.Text;
                    //        //////m.Flag = txt_UserName.Text;
                    //        //////m.ShowDialog();
                    //        //ClientForm m = new ClientForm();
                    //        //m.ShowDialog();

                    //        FrmRptMain rpt = new FrmRptMain();
                    //        rpt.ShowDialog();
                    //        this.Hide();
                    //        Application.Exit();



                    //}
                    //else
                    //{
                    //    MessageBox.Show("用户编码或用户密码不正确！", "软件提示");
                    //    txt_PassWord.Text = "";
                    //}

                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "软件提示");
                   
                }
            }
        }

        private void Login_Form_Activated(object sender, EventArgs e)
        {
            this.txt_UserName.Focus();
        }

        private void Login_Form_Load(object sender, EventArgs e)
        {


          
            this.MaximizeBox = false;//
        }

        private void btn_Login_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void txt_PassWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                button1_Click(sender, e);
            }
        }

        private void btn_Cloes_Click(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();
            this.Close();
        }
        public static string GenerateId()
        {
            lock (Locker)  //lock 关键字可确保当一个线程位于代码的临界区时，另一个线程不会进入该临界区。 
            {
                if (_sn == int.MaxValue)
                {
                    _sn = 0;
                }
                else
                {
                    _sn++;
                }

                Thread.Sleep(50);

                return  DateTime.Now.ToString("yyyyMMdd") + _sn.ToString().PadLeft(4, '0');
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pal_Login_Paint(object sender, PaintEventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker allInOneWorker = sender as BackgroundWorker;
            //int i = Convert.ToInt32(OleDbHelper3.ExecuteScalar("select count( *) from Operators where curr_user_id='" + txt_UserName.Text + "' and user_password='" + txt_PassWord.Text + "'", null));
            if (allInOneWorker.CancellationPending == true)
            {
                e.Cancel = true;
            }
            try
            {
                UserCount = Convert.ToInt32(SQLHelper2.ExecuteScalar("select count( *) from Operators where curr_user_id='" + txt_UserName.Text + "' and password='" + txt_PassWord.Text + "'", null));

            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message, "软件提示");
                UserCount = -1;

            }
            finally
            {
                backgroundWorker1.ReportProgress(100);
            }
        

          //backgroundWorker1.ReportProgress(100);



            //for (int i = 1; i <= 10; i++)
            //{
            //    if (allInOneWorker.CancellationPending == true)
            //    {
            //        e.Cancel = true;
            //        break;
            //    }
            //    else
            //    {
            //        System.Threading.Thread.Sleep(500);
            //        allInOneWorker.ReportProgress(i * 10);
            //    }
            //}
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
            //LbTip.Text = (e.ProgressPercentage.ToString() + "%"); 
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                //LbTip.Text = "已取消!";
            }
            else if (e.Error != null)
            {
                //LbTip.Text = "发生异常: " + e.Error.Message;
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                //LbTip.Text = "已完成!";

                skinProgressIndicator1.Hide();
                if (UserCount == -1)
                    return;
                if (UserCount > 0)
                {
                    GlobalVar.CurrOperator = txt_UserName.Text.Trim();//存储用户ID

                    this.Hide();
                    //////Main2 m = new Main2();
                    //////username = txt_UserName.Text;
                    //////m.Flag = txt_UserName.Text;
                    //////m.ShowDialog();
                    //ClientForm m = new ClientForm();
                    //m.ShowDialog();

                    FrmRptMain rpt = new FrmRptMain();
                    rpt.ShowDialog();
                    this.Hide();
                    Application.Exit(); 
                }
                else
                {
                    MessageBox.Show("用户编码或用户密码不正确！", "软件提示");
                    txt_PassWord.Text = "";
                }
            }
        }

        private void skinProgressIndicator1_Click(object sender, EventArgs e)
        {

        }
    } 
}

  
  
   /// <summary> 
   /// 生成订单编号 
   /// </summary> 
   /// <returns></returns> 
   
 

